package cn.fan.service;

import cn.fan.constants.DataBaseConstants;
import cn.fan.feign.clients.UserClient;
import cn.fan.feign.pojo.User;
import cn.fan.mapper.FundMainMapper;
import cn.fan.pojo.FundMain;
import cn.fan.pojo.rxiong.RXFundMain;
import cn.fan.redis.FundMainRedis;
import cn.fan.utils.R;
import cn.fan.utils.UserUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class FundMainService {
    // 更新（添加、删除、获取）失败
    private static final int FAIL = -1;

    private static final Long AUTO = 0L;

    @Autowired
    private FundMainMapper fundMainMapper;

    @Autowired
    private FundMainRedis fundMainRedis;

    @Autowired
    private UserClient userClient;

    @Autowired
    private RxiongService rxiongService;

    public User getSysUser(){
        R r =userClient.get(UserUtils.getSysUserId());
        System.out.println("R:"+r);

        String userJson = JSON.toJSONString(r.get("user"));
        User user = JSONObject.parseObject(userJson,User.class);
        System.out.println("user:"+user.toString());
        return user;
    }

    public FundMain getById(long id){
        return fundMainRedis.getById(id);
    }

    public FundMain getByCode(String code){
        return fundMainRedis.getByCode(code);
    }

//    public void add(List<FundMain> fundMains){
//        long sysUserId = UserUtils.getSysUserId();
//        if(sysUserId == FAIL)
//            return;
//
//        addFundMains(fundMains,sysUserId);
//    }

    public void addFundMains(FundMain fundMain,long sysUserId){
        fundMain.setCreateDate(new Date());
        fundMain.setCreateBy(sysUserId);

        fundMainMapper.insert(fundMain);
        fundMainRedis.putById(fundMain.getId(),fundMain);
        fundMainRedis.putByCode(fundMain.getCode(),fundMain);
        fundMainRedis.putList();
    }

    public int delete(long id){
        FundMain fundMain = fundMainRedis.getById(id);
        if(fundMain==null)
            return DataBaseConstants.NO_DATA;

        int i = fundMainMapper.deleteById(id);
        fundMainRedis.delete(id);
        fundMainRedis.deleteByCode(fundMain.getCode());
        fundMainRedis.putList();
        return i;
    }

    public int edit(FundMain fundMain){
        long sysUserId = UserUtils.getSysUserId();
        if(sysUserId == FAIL)
            return FAIL;

        FundMain fundMain1 = getById(fundMain.getId());

        fundMain1.setFundname(fundMain.getFundname());
        fundMain1.setCommissionRate(fundMain.getCommissionRate());
        fundMain1.setUpdateDate(new Date());
        fundMain1.setUpdateBy(sysUserId);

        int i = fundMainMapper.updateById(fundMain1);
        fundMainRedis.putById(fundMain1.getId(),fundMain1);
        fundMainRedis.putByCode(fundMain1.getCode(),fundMain1);
        fundMainRedis.putList();
        return i;
    }

    public List<FundMain> list(){
        return fundMainRedis.getList();
    }

    public List<FundMain> list(FundMain fundMain){
        LambdaQueryWrapper<FundMain> queryWrapper = getFundMainLambdaQueryWrapper(fundMain);
        List<FundMain> list =fundMainMapper.selectList(queryWrapper);
        return list;
    }

    public IPage<FundMain> page(FundMain fundMain,long currentPage,long pageSize){
        IPage<FundMain> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<FundMain> queryWrapper = getFundMainLambdaQueryWrapper(fundMain);
        IPage<FundMain> fundMainIPage = fundMainMapper.selectPage(page,queryWrapper);
        return fundMainIPage;
    }

    private LambdaQueryWrapper<FundMain> getFundMainLambdaQueryWrapper(FundMain fundMain) {
        LambdaQueryWrapper<FundMain> queryWrapper = new LambdaQueryWrapper<FundMain>();

        queryWrapper.eq(null!=fundMain.getId(),
                FundMain::getId,
                fundMain.getId());
        queryWrapper.like(null!=fundMain.getCode(),
                FundMain::getCode,
                fundMain.getCode());
        queryWrapper.like( StringUtils.hasText(fundMain.getFundname()),
                FundMain::getFundname,
                fundMain.getFundname());
        queryWrapper.like(null!=fundMain.getFundtype(),
                FundMain::getFundtype,
                fundMain.getFundtype());
        queryWrapper.eq(null!=fundMain.getCommissionRate(),
                FundMain::getCommissionRate,
                fundMain.getCommissionRate());
        return queryWrapper;
    }

    // 通过第三方同步数据
    public void synchro(String codes){
        List<RXFundMain> rxFundMains = rxiongService.getAll(codes);

        for(RXFundMain rxFundMain:rxFundMains){
            FundMain byCode = getByCode(rxFundMain.getId());
            if (byCode == null) {
                FundMain fundMain = new FundMain();
                fundMain.setCode(rxFundMain.getId());
                fundMain.setFundname(rxFundMain.getFundname());
                fundMain.setFundtype(rxFundMain.getType());

                addFundMains(fundMain, AUTO);
            }
        }
    }

    public void synchro(FundMain fundMain){
        List<RXFundMain> rxFundMains = rxiongService.getAll(fundMain.getCode());

        for(RXFundMain rxFundMain:rxFundMains){
            if(fundMain.getCode().equals(rxFundMain.getId())){
                FundMain byCode = getByCode(rxFundMain.getId());
                if (byCode == null) {
                    fundMain.setFundname(rxFundMain.getFundname());
                    fundMain.setFundtype(rxFundMain.getType());

                    addFundMains(fundMain, AUTO);
                }
            }
        }
    }

}
